package NonBinaryAttributes;
import java.util.ArrayList;


public class ItemSetList {
	private ArrayList<ItemSet> itemSets;
	private int size;
	
	public ItemSetList(int size) {
		itemSets = new ArrayList<ItemSet>();
		this.size = size;
	}

	public void addItemSet(ItemSet is) {
		itemSets.add(is);
	}

	@Override
	public String toString() {
		String s = "";
		for(ItemSet is:itemSets) {
			s+= is;
		}
		return s;
	}

	//generate the F size frequent itemsets using the (F-1)*(F-1) algorithm
	public ItemSetList generateNextSize(int minSupport) {
		//generation
		ItemSetList nextSize = new ItemSetList(size + 1);
		
		int coincidentSize = size - 1; //number of elements that have to be equal
		
		for(int i = 0; i < itemSets.size(); ++i) {
			int i2 = i+1;
			boolean possibleMatch = true;
			while(possibleMatch) {
				//we compare with next elements and merge them if possible
				ItemSet is1 = itemSets.get(i);
				if(i2 < itemSets.size() && is1.partialMatch(itemSets.get(i2), coincidentSize)) {
					ItemSet is2 = itemSets.get(i2);
					ItemSet merged = is1.merge(is2);
					
					//pruning
					merged.calculateSupport();
					if(merged.getSupport() >= minSupport) {
						merged.setFrequent(true);
						nextSize.addItemSet(merged);
					}
					is1.updateStatus();
					is2.updateStatus();					
				}
				else possibleMatch = false;
				i2++;
			}
		}		
		return nextSize;
	}

	public int getItemSetNumber() {
		return itemSets.size();
	}
}
